package com.summer.peony.sso.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.summer.peony.common.entity.AjaxResult;
import com.summer.peony.common.entity.ServiceException;
import com.summer.peony.sso.pojo.MessageProperties;
import com.summer.peony.sso.service.FileUpAndDownService;

import net.coobird.thumbnailator.Thumbnails;

/**
 * @ClassName: FileUpAndDownServiceImpl
 * @Description: TODO(上传图片实现层)
 * @author: TaoGe
 * @date: 2021年1月29日 下午1:56:02
 * @version: v0.0.1
 */
@Service
public class FileUpAndDownServiceImpl implements FileUpAndDownService {

	@Autowired
	private MessageProperties config; // 用来获取file-message.properties配置文件中的信息

	@Override
	public AjaxResult uploadPicture(MultipartFile file) throws ServiceException {
		// 上传图片
		try {
			AjaxResult ajaxResult = new AjaxResult();
			String[] IMAGE_TYPE = config.getImageType().split(",");
			String path = null;
			boolean flag = false;
			for (String type : IMAGE_TYPE) {
				if (StringUtils.endsWithIgnoreCase(file.getOriginalFilename(), type)) {
					flag = true;
					break;
				}
			}
			if (flag) {
				ajaxResult.put("code", 0);
				ajaxResult.put("msg", "上传成功");
				String uuid = UUID.randomUUID().toString().replaceAll("-", "");
				// 获得文件类型
				String fileType = file.getContentType();
				// 获得文件后缀名称
				String imageName = fileType.substring(fileType.indexOf("/") + 1);
				// 原名称
				String oldFileName = file.getOriginalFilename();
				// 新名称
				String newFileName = uuid + "." + imageName;
				// 年月日文件夹
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
				String basedir = sdf.format(new Date());
				// 进行压缩(大于4M)
				if (file.getSize() > config.getFileSize()) {
					// 重新生成
					String newUUID = UUID.randomUUID().toString().replaceAll("-", "");
					newFileName = newUUID + "." + imageName;
					path = config.getUpPath() + "/" + basedir + "/" + newUUID + "." + imageName;
					// 如果目录不存在则创建目录
					File oldFile = new File(path);
					if (!oldFile.exists()) {
						oldFile.mkdirs();
					}
					file.transferTo(oldFile);
					// 压缩图片
					Thumbnails.of(oldFile).scale(config.getScaleRatio()).toFile(path);
					// 显示路径
					ajaxResult.put("path", "/" + basedir + "/" + newUUID + "." + imageName);
				} else {
					path = config.getUpPath() + "/" + basedir + "/" + uuid + "." + imageName;
					// 如果目录不存在则创建目录
					File uploadFile = new File(path);
					if (!uploadFile.exists()) {
						uploadFile.mkdirs();
					}
					file.transferTo(uploadFile);
					// 显示路径
					ajaxResult.put("path", "/" + basedir + "/" + uuid + "." + imageName);
				}
				ajaxResult.put("oldFileName", oldFileName);
				ajaxResult.put("newFileName", newFileName);
				ajaxResult.put("fileSize", file.getSize());
			} else {
				ajaxResult.put("code", 900013);
				ajaxResult.put("msg", "图片格式不正确,支持png|jpg|jpeg");
			}
			return ajaxResult;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
}